using Gl.SettingsManager.Providers.DbSettings;
using System.Collections.Generic;
namespace Gl.SettingsManager.Providers
{
	internal class DataBaseSettingsProvider : ISettingsProvider
	{
		private DbSettingsManager _sManager;
		private List<SettingsCollection> _loaded = new List<SettingsCollection>();

		private string _connection;
		private bool _useTrasaction;
		private string _applicationName;

		public DataBaseSettingsProvider(string connection, bool useTransactionWhileSaving, string applicationName)
		{
			_connection = connection;
			_useTrasaction = useTransactionWhileSaving;
			_applicationName = applicationName;
		}

		#region ISettingsProvider Members

		SettingsCollection ISettingsProvider.GetSettings(string typeName, string context)
		{
			SettingsCollection loadedCollection = _sManager.GetSettings(typeName, context);
			_loaded.Add(loadedCollection);
			return loadedCollection;
		}

		void ISettingsProvider.Save()
		{
			foreach (SettingsCollection coll in _loaded)
				_sManager.SaveSettings(coll);
		}

		void ISettingsProvider.InitializeProvider()
		{
			_sManager = new DbSettingsManager(_useTrasaction, _applicationName, _connection);
		}

		bool ISettingsProvider.IsInitialized
		{
			get { return _sManager != null; }
		}

		void ISettingsProvider.ResetSettings()
		{
			_sManager.ResetSettings();
		}

		#endregion
	}
}
